import { RSAUtils } from "./rsaUtils";

//公钥加密，私钥解密
async function test() {
  try {
    const keyPair = await RSAUtils.genKeyPair();
    const privateKey = keyPair[RSAUtils.PRIVATE_KEY];
    const publicKey = keyPair[RSAUtils.PUBLIC_KEY];

    const loginName = JSON.stringify({ loginName: "leo", password: "123456" });

    // const jsonObject: { [key: string]: string } = { loginName };
    // console.log("加密前JSON：", JSON.stringify(jsonObject));
    // 使用公钥加密
    const encryptedLoginName = await RSAUtils.encryptByPublicKey(
      Buffer.from(loginName),
      publicKey
    );
    const encryptedLoginNameBase64 = encryptedLoginName.toString("base64");
    // jsonObject.loginName = encryptedLoginNameBase64;
    console.log("加密后loginName：", encryptedLoginNameBase64);
    // console.log("加密后JSON：", JSON.stringify(jsonObject));
    console.log("-----");

    // 接受后解密还原
    const encryptedLoginName2 = Buffer.from(encryptedLoginNameBase64, "base64");
    console.log("解密前loginName：", encryptedLoginNameBase64);
    // 使用私钥解密
    const decryptedData = await RSAUtils.decryptByPrivateKey(
      encryptedLoginName2,
      privateKey
    );
    console.log("解密后：", decryptedData.toString());
    const decryptedDataJson = JSON.parse(decryptedData.toString());
    console.log("解密后JSON：", decryptedDataJson.loginName);
  } catch (error) {
    console.error("执行过程中出现错误：", error);
  }
}
//私钥加密，公钥解密
async function test2() {
  try {
    const keyPair = await RSAUtils.genKeyPair();
    const privateKey = keyPair[RSAUtils.PRIVATE_KEY];
    const publicKey = keyPair[RSAUtils.PUBLIC_KEY];

    const loginName = JSON.stringify({ loginName: "leo", password: "123456" });

    // 使用私钥加密
    const encryptedLoginName = await RSAUtils.encryptByPrivateKey(
      Buffer.from(loginName),
      privateKey
    );
    const encryptedLoginNameBase64 = encryptedLoginName.toString("base64");

    console.log("加密后loginName：", encryptedLoginNameBase64);

    console.log("----------------------------------");

    // 接受后解密还原
    const encryptedLoginName2 = Buffer.from(encryptedLoginNameBase64, "base64");
    console.log("解密前loginName：", encryptedLoginNameBase64);
    // 使用公钥解密
    const decryptedData = await RSAUtils.decryptByPublicKey(
      encryptedLoginName2,
      publicKey
    );
    console.log("解密后：", decryptedData.toString());
    const decryptedDataJson = JSON.parse(decryptedData.toString());
    console.log("解密后JSON：", decryptedDataJson.loginName);
  } catch (error) {
    console.error("执行过程中出现错误：", error);
  }
}
async function test3() {
  try {
    //加签验签
    const keyPair = await RSAUtils.genKeyPair();
    const privateKey = keyPair[RSAUtils.PRIVATE_KEY];
    const publicKey = keyPair[RSAUtils.PUBLIC_KEY];

    const loginName = JSON.stringify({ loginName: "leo", password: "123456" });

    // 使用私钥加密
    const encryptedLoginName = await RSAUtils.encryptByPrivateKey(
      Buffer.from(loginName),
      privateKey
    );

    const sign = await RSAUtils.sign(
      encryptedLoginName.toString("base64"),
      privateKey
    );
    console.log("签名：", sign);
    const verify = await RSAUtils.verify(
      encryptedLoginName.toString("base64"),
      publicKey,
      sign
    );
    console.log("验签：", verify);
  } catch (error) {
    console.error("执行过程中出现错误：", error);
  }
}
(async () => {
  // await test();
  // console.log("================================");
  // await test2();
  await test3();
})();
